package com.sch.example.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatCheckBox;

import com.sch.example.R;
import com.sch.example.util.EmptyUtils;


/**
 * 重复设置Window
 */
public class RepeatWindow extends PopupWindow {

    // 通用
    private Context mContext;

    private TextView mBtnRe, mBtnOK;

    private AppCompatCheckBox
            mCheckBox_7,
            mCheckBox_1,
            mCheckBox_2,
            mCheckBox_3,
            mCheckBox_4,
            mCheckBox_5,
            mCheckBox_6;

    /**
     * 构造方法
     */
    public RepeatWindow(Activity mContext) {
        super(mContext);
        View view = LayoutInflater.from(mContext).inflate(R.layout.window_repeat, null);

        this.mContext = mContext;
        this.setContentView(view);                                 // 设置SelectPicPopupWindow的View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);       // 设置SelectPicPopupWindow弹出窗体的高
        this.setAnimationStyle(R.style.AnimBottom);                // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setBackgroundDrawable(new ColorDrawable(0xb0000000)); // 设置SelectPicPopupWindow弹出窗体的背景

        this.setFocusable(true);
        this.setTouchable(true);
        this.setOutsideTouchable(true);

        initCheck(view);
    }


    /**
     * 确定键监听器
     */
    public void setOnBtnListener(View.OnClickListener listener) {
        mBtnRe.setOnClickListener(listener);
        mBtnOK.setOnClickListener(listener);
    }


    private String[] mArr_week = {"", "", "", "", "", "", ""};


    /**
     * 创建
     */
    private void initCheck(View view) {
        mBtnRe = view.findViewById(R.id.btn_cancel);
        mBtnOK = view.findViewById(R.id.btn_ok);

        mCheckBox_7 = view.findViewById(R.id.checkbox_7);
        mCheckBox_1 = view.findViewById(R.id.checkbox_1);
        mCheckBox_2 = view.findViewById(R.id.checkbox_2);
        mCheckBox_3 = view.findViewById(R.id.checkbox_3);
        mCheckBox_4 = view.findViewById(R.id.checkbox_4);
        mCheckBox_5 = view.findViewById(R.id.checkbox_5);
        mCheckBox_6 = view.findViewById(R.id.checkbox_6);

        mCheckBox_7.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_7 = isChecked ? "周日" : "";
            mArr_week[6] = isChecked_7;
        });
        mCheckBox_1.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_1 = isChecked ? "周一" : "";
            mArr_week[0] = isChecked_1;
        });
        mCheckBox_2.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_2 = isChecked ? "周二" : "";
            mArr_week[1] = isChecked_2;
        });
        mCheckBox_3.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_3 = isChecked ? "周三" : "";
            mArr_week[2] = isChecked_3;
        });
        mCheckBox_4.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_4 = isChecked ? "周四" : "";
            mArr_week[3] = isChecked_4;
        });
        mCheckBox_5.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_5 = isChecked ? "周五" : "";
            mArr_week[4] = isChecked_5;
        });
        mCheckBox_6.setOnCheckedChangeListener((buttonView, isChecked) -> {
            String isChecked_6 = isChecked ? "周六" : "";
            mArr_week[5] = isChecked_6;
        });

    }


//    /**
//     * 设置初始值
//     */
//    public void setInitValue(int indexLeft,int indexRight){
////        mLeftWheel.setCurrentItem(indexLeft);
////        mRightWheel.setCurrentItem(indexRight);
//    }

    /**
     * 获取 星期数组
     */
    public String[] getWeekArr() {
        return mArr_week;
    }


    /**
     * 获得 星期数组 字符串
     */
    public String getWeekString() {

        StringBuilder strB = new StringBuilder();

        for (int i = 0; i < mArr_week.length; i++) {

            // 不是空字符串
            if (!EmptyUtils.isEmpty(mArr_week[i])) {

                if (i == mArr_week.length - 1) {
                    strB.append(mArr_week[i]);
                } else {
                    strB.append(mArr_week[i]).append(",");
                }
            }

        }

        String value = strB.substring(strB.length() - 1);

        // 删除最后一个逗号
        if (value.equals(",")) {
            return strB.substring(0,strB.length() - 1);
        } else {
            return strB.toString();
        }


    }

    /**
     * 获取 选择 数量
     */
    public int getAll_index(){

        int all_index = 0;

        for (String s : mArr_week) {

            // 不是空字符串
            if (!EmptyUtils.isEmpty(s)) {
                all_index = all_index + 1;
            }

        }
        return all_index;
    }

}
